//
//  SetViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/22.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "SetViewController.h"
#import "RentVieitor.h"
#import "LCMD5Tool.h"
#import "AboutMySelfViewController.h"
#import "PushSetViewController.h"
#import "QRcodeViewController.h"

@interface SetViewController ()

@end

@implementation SetViewController {
    MBProgressHUD *HUD;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView ];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 10,15 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 20, 17);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"消息"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(noticeAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)creatView {
    
//    //分割线
//    CALayer *MlineLayer7 = [CALayer layer];
//    MlineLayer7.backgroundColor = [[Utils colorConvertFromString:@"#d0d0d0"] CGColor];
//    MlineLayer7.frame = CGRectMake(0 ,64, KWidth, 10);
//    [self.view.layer addSublayer:MlineLayer7];
    NSArray *titleArray = @[@"推送设置",@"清理缓存",@"关于我们",@"版权信息",@"推荐二维码",@"去评分",@"退出登录"];
    //选项视图
    CGFloat heigt = 50;
    for (int i=0; i<titleArray.count; i++) {
        
        UIView *selectView = [[UIView alloc] init];
        selectView.backgroundColor = [UIColor whiteColor];
        selectView.tag = 100+i;
        if (i<2 ) {
            
            selectView.frame = CGRectMake(0,10+heigt*i , KWidth, heigt);
            
        }else if (i>1 &i<6) {
            
             selectView.frame = CGRectMake(0, heigt*i+20, KWidth, heigt);
        }
        else {
            
            selectView.frame = CGRectMake(0, heigt*i+30, KWidth, heigt);
        }
        [self.view addSubview:selectView];
        
        //添加手势
        UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
        PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
        PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
        [selectView addGestureRecognizer:PrivateLetterTap];
        
        UILabel *selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, KWidth/2, 30)];
        selectLabel.text = titleArray[i];
        selectLabel.font = [UIFont systemFontOfSize:15];
        selectLabel.textAlignment = NSTextAlignmentLeft;
        selectLabel.textColor = [Utils colorConvertFromString:@"#101010"];
        [selectView addSubview:selectLabel];
        
        UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        selectButton.frame = CGRectMake(KWidth-25, 20, 5, 10);
        [selectButton setBackgroundImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
        [selectView addSubview:selectButton];
        

        //分割线
        CALayer *MlineLayer5 = [CALayer layer];
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#cfcfcf"] CGColor];
        if (i == 1 || i== 5 || i == 6) {
            MlineLayer5.frame = CGRectMake(0 ,heigt-1, KWidth,10);
            MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#F0F0F0"] CGColor];
        }else {
            MlineLayer5.frame = CGRectMake(20 ,heigt-1, KWidth-40,1);
        }
        
        [selectView.layer addSublayer:MlineLayer5];
    }

}

- (void)tapAvatarView:(UIGestureRecognizer *)recognizer {
    
    if (recognizer.view.tag == 100) {
        
        //推送设置
        PushSetViewController *pushVc = [[PushSetViewController alloc] init];
        [self.navigationController pushViewController:pushVc animated:YES];
        
    }else if (recognizer.view.tag == 101){
        
        //清理缓存
        [APPDELEGATE showPro:@"功能完善中"];
        
    }else if (recognizer.view.tag == 102){
        
        //关于我们
        AboutMySelfViewController *aboutVc = [[AboutMySelfViewController alloc] init];
        [self.navigationController pushViewController:aboutVc animated:YES];
        
    }else if (recognizer.view.tag == 103){
        //版权信息
         [APPDELEGATE showPro:@"功能完善中"];
        
    }else if (recognizer.view.tag == 104){
        
        //推荐二维码
        QRcodeViewController *qrVc = [[QRcodeViewController alloc] init];
        [self.navigationController pushViewController:qrVc animated:YES];
        
    }else if (recognizer.view.tag == 105){
        //去评分
         [APPDELEGATE showPro:@"功能完善中"];
        
    }else if (recognizer.view.tag == 106){
        //退出登录
        NSDictionary *dataDic = User_listsValue;
        NSString *number = dataDic[@"number"];
        NSString *token = dataDic[@"token"];
        if (number == nil || token == nil) {
            
            return;
        }
        
        NSString *urlstr= @"http://api2.zuke.com/login/logout";
        NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@",number,token,APPKEY_URL,urlstr];
        //32位小写
        md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
        NSDictionary *param = @{
                                
                                @"number":number,
                                @"token":token,
                                @"sign":md5String
                                
                                };
        
        __weak typeof(self)weakSelf = self;
        AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
        manger.responseSerializer=[AFHTTPResponseSerializer serializer];
        [manger POST:urlstr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            
            NSDictionary* jsonDic = [NSJSONSerialization
                                     JSONObjectWithData:responseObject
                                     options:kNilOptions
                                     error:nil];
            
            if ([jsonDic[@"code"] isEqualToString:@"10000"]) {
                
                //退出登录
                NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
                [defaults removeObjectForKey:User_listsKey];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil userInfo:nil];
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
            }else {
                
                [APPDELEGATE showPro:jsonDic[@"message"]];
            }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [APPDELEGATE showPro:@"网络错误"];
            
        }];
    }
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)noticeAction {
    
     [APPDELEGATE showPro:@"功能完善中"];
    
}
@end
